@echo off
rem 设置代码页为UTF-8以支持中文
chcp 65001 > nul
setlocal enabledelayedexpansion

rem 设置项目基本信息
set PROJECT_NAME=Image Cutter
set VENV_NAME=image_cutter_env
set PYTHON_VERSION=3.9
set PIP_CONFIG_FILE=%CD%\pip.conf

rem 设置标题
title Image Cutter Manager

:MainMenu
cls
echo =====================================================
echo              Image Cutter Manager v1.2
echo =====================================================
echo.
echo  [Run Options]
echo    1. Run Image Cutter (from root)
echo    2. Run Image Cutter (from src)
echo.
echo  [Environment]
echo    3. Create virtual environment and install dependencies
echo    4. Update dependencies
echo    5. Run tests
echo.
echo  [Development]
echo    6. Clean project cache files
echo    7. View changelog
echo    8. Start Python shell (with project path)
echo.
echo  [Other]
echo    9. Help
echo    0. Exit
echo.
echo =====================================================

set /p choice=Select option (0-9): 

if "%choice%"=="1" goto RunFromRoot
if "%choice%"=="2" goto RunFromSrc
if "%choice%"=="3" goto CreateEnv
if "%choice%"=="4" goto UpdateDeps
if "%choice%"=="5" goto RunTests
if "%choice%"=="6" goto Cleanup
if "%choice%"=="7" goto ViewLogs
if "%choice%"=="8" goto PythonShell
if "%choice%"=="9" goto Help
if "%choice%"=="0" goto Exit

echo Invalid choice, please try again!
timeout /t 2 >nul
goto MainMenu

:RunFromRoot
cls
echo Starting Image Cutter (from root)...
echo.

rem 检查虚拟环境
if exist image_cutter_env\Scripts\activate (
    echo Running with virtual environment...
    call image_cutter_env\Scripts\activate
    python run.py
    call image_cutter_env\Scripts\deactivate
) else (
    echo Running with system Python...
    python run.py
)

echo.
echo Program exited. Press any key to return to main menu...
pause >nul
goto MainMenu

:RunFromSrc
cls
echo Starting Image Cutter (from src)...
echo.

rem 检查虚拟环境
if exist image_cutter_env\Scripts\activate (
    echo Running with virtual environment...
    call image_cutter_env\Scripts\activate
    cd src
    python run_from_src.py
    cd ..
    call image_cutter_env\Scripts\deactivate
) else (
    echo Running with system Python...
    cd src
    python run_from_src.py
    cd ..
)

echo.
echo Program exited. Press any key to return to main menu...
pause >nul
goto MainMenu

:CreateEnv
cls
echo Creating virtual environment and installing dependencies...
echo.

rem 检查Python版本
python --version
if %ERRORLEVEL% neq 0 (
    echo Python is not installed or not in PATH. Please install Python 3.6+
    pause
    goto MainMenu
)

rem 创建虚拟环境
if exist image_cutter_env (
    echo Virtual environment exists, skipping creation...
) else (
    echo Creating virtual environment...
    python -m venv image_cutter_env
)

rem 安装依赖
echo Installing dependencies...
call image_cutter_env\Scripts\activate
python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
call image_cutter_env\Scripts\deactivate

echo.
echo Virtual environment created and dependencies installed. Press any key to return...
pause >nul
goto MainMenu

:UpdateDeps
cls
echo Updating dependencies...
echo.

if not exist image_cutter_env (
    echo Virtual environment not found. Please create it first!
    timeout /t 3 >nul
    goto MainMenu
)

call image_cutter_env\Scripts\activate
python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
pip install -r requirements.txt --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
call image_cutter_env\Scripts\deactivate

echo.
echo Dependencies updated. Press any key to return...
pause >nul
goto MainMenu

:RunTests
cls
echo Running tests...
echo.

if not exist image_cutter_env (
    echo Virtual environment not found. Please create it first!
    timeout /t 3 >nul
    goto MainMenu
)

call image_cutter_env\Scripts\activate
python -m unittest discover tests
call image_cutter_env\Scripts\deactivate

echo.
echo Tests completed. Press any key to return...
pause >nul
goto MainMenu

:Cleanup
cls
echo Cleaning project cache files...
echo.

echo Removing Python cache files and directories...

rem 删除__pycache__目录
for /d /r . %%d in (__pycache__) do @if exist "%%d" (
    echo Removing: %%d
    rd /s /q "%%d"
)

rem 删除.pyc文件
for /r . %%f in (*.pyc) do (
    echo Removing: %%f
    del /q "%%f"
)

rem 删除.pyo文件
for /r . %%f in (*.pyo) do (
    echo Removing: %%f
    del /q "%%f"
)

echo.
echo Cleanup completed. Press any key to return...
pause >nul
goto MainMenu

:ViewLogs
cls
echo Viewing changelog...
echo.

if exist "changelog.txt" (
    type "changelog.txt"
) else (
    echo Changelog file not found!
)

echo.
echo Press any key to return to main menu...
pause >nul
goto MainMenu

:PythonShell
cls
echo Starting Python interactive shell...
echo.

if exist image_cutter_env (
    call image_cutter_env\Scripts\activate
    python
    call image_cutter_env\Scripts\deactivate
) else (
    python
)

echo.
echo Python shell exited. Press any key to return...
pause >nul
goto MainMenu

:Help
cls
echo Image Cutter - Help
echo =====================================================
echo.
echo  Image Cutter is a tool for cutting the same content
echo  from multiple images.
echo.
echo  Main Features:
echo  - Multi-region selection and cutting
echo  - Smart region detection
echo  - Batch processing
echo  - Custom export options
echo  - Fine-tuning with zoom
echo.
echo  How to use:
echo  1. Select "Run Image Cutter" from menu
echo  2. Use "File" menu to open images
echo  3. Drag on image to select regions
echo  4. Use right-click menu for region operations
echo  5. Double-click region for fine-tuning mode
echo  6. Use "File" menu to save regions
echo.
echo =====================================================
echo.
echo Press any key to return to main menu...
pause >nul
goto MainMenu

:Exit
cls
echo Thanks for using Image Cutter Manager. Goodbye!
timeout /t 2 >nul
exit

endlocal 